package com.jiangruan.javaproject.service;

import com.alibaba.fastjson.JSONObject;
import com.jiangruan.javaproject.Constants;
import com.jiangruan.javaproject.request.HttpUtils;


import java.net.URLEncoder;

/**
 * @author zhongjun
 * @ClassName com.jiangruan.javaproject.service.WeatherService
 * @create 2021年12月22日 10:24
 * @desc:
 */
public class WeatherService {
    /**
     * 该方法用获取天气数据，并返回天气数据
     * @return
     */
    public WeatherCommon GetWeather(String city){
        String citys = URLEncoder.encode(city);
        String url = Constants.WEATHER_URL
                + "?city=" + citys
                + "&key="+Constants.WEATHER_KEY;
        String result = HttpUtils.get(url);
        System.out.println(result);
        WeatherCommon weather = JSONObject.parseObject(result, WeatherCommon.class);
        return weather;
    }
}
